<template>
    <div class="edu-page-warp">
        <!-- <el-card shadow="never" body-style="padding: 24px;"> -->
        <!-- <div style="height:50px">
                <div
                    class="edu-page-title pull-left"
                    style="display:flex;flex-direction:row;align-items:center"
                >
                    <span>{{$t('message.filterCondition')}}</span>
                    <div style="display:flex;flex-direction:row;align-items:center" @click="fold">
                        <i
                            :class="filterBlockStatus?'el-icon-arrow-down':'el-icon-arrow-up'"
                            style="font-size:13px;color:#3A7FE8;margin-right:5px;font-weight:bold;"
                        ></i>
                        <span v-if="filterBlockStatus" style="color:#3A7FE8;font-size:12px;">收起</span>
                        <span v-else style="color:#3A7FE8;font-size:12px;">展开</span>
                    </div>
                </div>
                <div class="pull-right">
                    <el-button size="mini" @click="resetSearch">重置</el-button>
                    <el-button size="mini" type="primary" @click="searchApplyNameList">筛选</el-button>
                </div>
        </div>-->
        <condition-card
            @reset="resetSearch"
            @search="searchApplyNameList"
            body-style="padding: 24px 24px 6px 24px;"
        >
            <!--事件-->
            <el-form v-show="filterBlockStatus" label-position="right" label-width="80px">
                <el-row :gutter="24">
                    <el-col :span="8">
                        <el-form-item label="辅修年级:" prop="grade">
                            <!-- <el-date-picker
                                style="width:100%"
                                v-model="queryParams.grade"
                                type="year"
                                readonly
                                value-format="yyyy"
                                placeholder="选择年级"
                            ></el-date-picker>-->
                            <el-input style="width:100%" v-model="queryParams.grade" readonly></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="学校:" prop="schoolId">
                            <el-select
                                clearable
                                v-model="queryParams.schoolId"
                                placeholder="请选择学校"
                                @change="queryAdvancedLevelBySchool"
                                :filterable="true"
                                style="width:100%;"
                            >
                                <el-option
                                    v-for="item in options.schoolId"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="辅修专业:" prop="minorMajorId">
                            <el-select
                                clearable
                                v-model="form.minorMajorId"
                                placeholder="请选择"
                                :filterable="true"
                                @change="clearTabelData"
                                style="width:100%;"
                            >
                                <el-option
                                    v-for="item in options.minorMajorId"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="24">
                    <el-col :span="8">
                        <el-form-item label="进阶层次:">
                            <el-select
                                clearable
                                v-model="form.advancedLevel"
                                style="width:100%;"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in options.advancedLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="姓名:" prop="name">
                            <el-input
                                v-model="form.name"
                                autocomplete="off"
                                placeholder="请输入姓名"
                                style="width:100%;"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="学号:" prop="studentNum">
                            <el-input
                                v-model="form.studentNum"
                                placeholder="请输入学号"
                                autocomplete="off"
                                style="width:100%;"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="24">
                    <el-col :span="8">
                        <el-form-item label="录取状态:" prop="recruitStatus">
                            <el-select
                                clearable
                                v-model="form.recruitStatus"
                                placeholder="请选择"
                                style="width:100%;"
                            >
                                <el-option
                                    v-for="item in options.recruitStatus"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </condition-card>
        <div style="height: 2px;background-color: #D2D4D6;"></div>
        <el-card shadow="never" body-style="padding: 24px;">
            <div style="width:100%;">
                <div class="addButon">
                    <el-dropdown placement="bottom-start">
                        <el-button>
                            导出
                            <i class="el-icon-arrow-down el-icon--right"></i>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="exportCurrentFormExcel">导出当前表单</el-dropdown-item>
                            <el-dropdown-item @click.native="exportSchoolOutMinorListExcel">导出全部校外辅修</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                    <el-button @click="cancelMatriculateLastMinor" v-show="isOptionStatus">取消录取</el-button>
                    <el-button
                        type="primary"
                        @click="batchMatriculateLastMinor"
                        v-show="isOptionStatus"
                    >批量录取</el-button>
                    <el-button
                        type="primary"
                        @click="matriculateLastMinor"
                        v-show="isOptionStatus"
                    >录取</el-button>
                </div>
                <div v-if="formShow" class="signFont">
                    <div class="divbackground"></div>
                    <p>请输入查询条件选取名单</p>
                </div>
                <el-table
                    :data="tableData"
                    v-else
                    @selection-change="chooseRow"
                    tooltip-effect="light"
                >
                    <el-table-column prop="schoolMinorId" type="selection"></el-table-column>
                    <el-table-column prop="entranceTerm" label="辅修年级" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="studentNum" label="学号" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="name" label="姓名" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="faulty" label="所属学院" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="cultrueProfessionI18n" label="所属专业" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="advancedLevelName" label="进阶层次" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="majorNameZn" label="辅修专业" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="minorGradePoint" label="辅修绩点" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="totalAvgGradePoint" label="总平均绩点" show-overflow-tooltip></el-table-column>
                    <el-table-column
                        prop="prerequisiteTagId"
                        label="先修课（绩点）"
                        show-overflow-tooltip
                        min-width="100px"
                    ></el-table-column>
                    <el-table-column prop="attachId" label="附件">
                        <template slot-scope="scope">
                            <span
                                v-if="!!scope.row.attachId"
                                class="preview-btn"
                                v-viewer="toolOptions"
                            >
                                查看附件
                                <img :src="scope.row.imgUrl" />
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="recruitStatus" label="录取状态">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="scope.row.recruitStatus*1 == 1"
                                type="success"
                                size="mini"
                            >已录取</el-tag>
                            <el-tag v-else type="danger" size="mini">未录取</el-tag>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div style="margin: 18px 0;">
                <div style="float:right;">
                    <el-pagination
                        v-nocursor
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="page.currentPage"
                        :page-sizes="[20,50,100,500,1000]"
                        :page-size="page.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="page.totalPage"
                    ></el-pagination>
                </div>
            </div>
        </el-card>
    </div>
</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import Calendars from "common/src/api/calendars";
import { getSessionUser } from "common/src/api/session";
import UTILS from "common/src/utils/utils";
import {
    queryAdvancedLevel,
    queryMinorMajorInfo,
    queryUniversityDicByIsOnCampus,
    queryMinorConfigInfo
} from "common/src/api/minor/collegemajor";
import {
    queryApplyMinorSignUpList,
    matriculate,
    cancelMatriculate,
    batchMatriculate,
    exportStudentListByAdmin
} from "common/src/api/minor/lastMinorSignUpList";
import nocursor from "common/src/directives/nocursor";
export default {
    data() {
        return {
            filterBlockStatus: true,
            dialogTableVisible: false,
            formShow: true,
            isOptionStatus: true,
            chooseRows: [],
            schoolMinorIds: "",
            schoolList: [],
            allowOption: false,
            form: {
                name: "",
                school: "",
                studentNum: "",
                recruitStatus: "",
                advancedLevel: "",
                majorName: "",
                minorMajorId: "",
                prePoint: "",
                gradeLimit: "",
                recruitRemark: "",
                list: [{ tagName: "", proprePoint: "" }]
            },
            options: {
                grade: [],
                advancedLevel: [],
                minorMajorId: [],
                schoolId: [],
                recruitStatus: [
                    {
                        value: "0",
                        label: "未录取"
                    },
                    {
                        value: "1",
                        label: "已录取"
                    }
                ]
            },
            tableData: [],
            queryParams: {
                schoolId: "",
                pageNum_: 1,
                pageSize_: 20,
                grade: ""
            },
            page: {
                currentPage: 1,
                pageSize: 20,
                totalPage: 0
            },
            updateParams: {
                advancedLevel: "",
                majorCode: "",
                minorSchool: "",
                faultyCode: ""
            },
            toolOptions: {
                toolbar: {
                    zoomIn: 4,
                    zoomOut: 4,
                    oneToOne: 4,
                    reset: 0,
                    prev: 0,
                    play: {
                        show: 4,
                        size: "large"
                    },
                    next: 0,
                    rotateLeft: 4,
                    rotateRight: 4,
                    flipHorizontal: 4,
                    flipVertical: 4
                }
            } //预览工具栏设置
        };
    },
    directives: {
        nocursor
    },
    created() {},
    mounted() {
        if (!this.$checkMinor()) return;
        queryMinorConfigInfo("").then(res => {
            if (res.code == 200) {
                this.minorGrade = res.data[0].grade;
                this.queryParams.grade = res.data[0].grade;
                //校验时间是否在报名结束时间和查询开始时间之间，可以进行操作，否在不能操作
                if (
                    res.data[0].signSwitchEndTime <= new Date().getTime() &&
                    new Date().getTime() <= res.data[0].querySwitchStartTime
                ) {
                    this.isOptionStatus = true;
                } else {
                    this.isOptionStatus = false;
                }
            }
        });
        this.initSchoolList();
    },
    methods: {
        initSchoolList() {
            let params = {
                isOnCampus: ""
            };
            queryUniversityDicByIsOnCampus(params).then(res => {
                if (res.code == 200) {
                    for (let key = 0; key < res.data.length; key++) {
                        this.options["schoolId"].push({
                            value: res.data[key].minorUniversityCode,
                            label: res.data[key].minorUniversityName
                        });
                    }
                    this.schoolList = res.data;
                }
            });
        },
        initAdvancedLevel(isOnCampus) {
            let params = {
                isOnCampus: isOnCampus ? isOnCampus : "0"
            };
            this.options["advancedLevel"] = [];
            queryAdvancedLevel(params).then(res => {
                if (res.code == 200) {
                    for (let key = 0; key < res.data.length; key++) {
                        this.options["advancedLevel"].push({
                            value: res.data[key].advancedLevelCode,
                            label: res.data[key].advancedLevelName
                        });
                    }
                }
            });
        },
        queryMinorMajorCondition() {
            this.allowOption = false;
            this.tableData = [];
            let params = {
                schoolId: this.queryParams.schoolId
            };
            this.form.minorMajorId = "";
            this.queryMinorMajorqueryMinorMajor(params);
        },
        queryMinorMajorqueryMinorMajor(params) {
            if (!this.$checkMinor()) return;
            this.options["minorMajorId"] = [];
            params.status = 1;
            params.minorGrade = this.minorGrade;
            queryMinorMajorInfo(params).then(res => {
                if (res.code == 200) {
                    for (let key = 0; key < res.data.list.length; key++) {
                        this.options["minorMajorId"].push({
                            value: res.data.list[key].majorCode,
                            label: res.data.list[key].majorNameZh
                        });
                    }
                }
            });
        },
        //表格复选框选中或取消
        chooseRow(val) {
            this.chooseRows = val;
        },
        fold() {
            this.filterBlockStatus = !this.filterBlockStatus;
        },
        addPreTag() {
            this.form.list.push({ tagName: "", proprePoint: "" });
        },
        delPreTag(i) {
            if (this.form.list.length > 1) {
                this.form.list.splice(i, 1);
            }
        },
        searchApplyNameList() {
            this.allowOption = true;
            //校验辅修年级、进阶层次、辅修专业为必填项
            if (
                this.queryParams.grade == "" ||
                this.queryParams.grade == undefined
            ) {
                this.$message.warning("请选择辅修年级");
            } else if (
                this.queryParams.schoolId == "" ||
                this.queryParams.schoolId == undefined
            ) {
                this.$message.warning("请选择学校");
            } else if (
                this.form.minorMajorId == "" ||
                this.form.minorMajorId == undefined
            ) {
                this.$message.warning("请选择专业");
            } else {
                this.formShow = false;
                let params = {
                    minorGrade: this.queryParams.grade,
                    advanceLevel: this.form.advancedLevel,
                    majorId: this.form.minorMajorId,
                    minorSchool: this.queryParams.schoolId,
                    name: this.form.name,
                    studentNum: this.form.studentNum,
                    matriculateStatus: this.form.recruitStatus,
                    pageNum: this.queryParams.pageNum_,
                    pageSize: this.queryParams.pageSize_
                };
                //为了解决ie取不到地址前缀
                /(^(http|https):\/\/[^\/]+)/.test(window.location.href);
                const origin = RegExp.$1;
                queryApplyMinorSignUpList(params).then(res => {
                    if (res.code == 200) {
                        res.data.list.forEach((i, index) => {
                            if (i.attachId) {
                                res.data.list[index].imgUrl =
                                    origin +
                                    "/api/commonservice/obsfile/downloadfile?objectkey=" +
                                    i.attachId;
                            }
                        });
                        this.tableData = res.data.list;
                        this.page.currentPage = res.data.pageNum_;
                        this.page.totalPage = res.data.total_;
                    }
                });
            }
        },
        resetSearch() {
            this.tableData = [];
            this.form.advancedLevel = "";
            this.form.minorMajorId = "";
            this.queryParams.schoolId = "";
            this.form.name = "";
            this.form.studentNum = "";
            this.form.recruitStatus = "";
            this.options.advancedLevel = [];
            this.options.minorMajorId = [];
            this.formShow = true;
            this.tableData = [];
            //this.searchApplyNameList();
        },
        handleCurrentChange(val) {
            this.queryParams.pageNum_ = val;
            this.searchApplyNameList();
        },
        queryAdvancedLevelBySchool() {
            this.allowOption = false;
            this.tableData = [];
            let isOnCampus = "";
            this.schoolList.length > 0 &&
                this.schoolList.forEach(e => {
                    if (e.minorUniversityCode == this.queryParams.schoolId) {
                        isOnCampus = e.isOnCampus;
                    }
                });
            this.options["advancedLevel"] = [];
            this.options["minorMajorId"] = [];
            this.form.advancedLevel = "";
            this.form.minorMajorId = "";
            this.initAdvancedLevel(isOnCampus);
            this.queryMinorMajorCondition();
        },
        cancelMatriculateLastMinor() {
            if (!this.$checkMinor()) return;
            if (this.allowOption) {
                this.schoolMinorIds = "";
                this.getSelectRows();
                if (this.schoolMinorIds.length == 0) {
                    this.$message.warning("请选择要取消录取的学生");
                    return;
                }
                if (this.isNotMatriculated) {
                    this.$message({
                        type: "warning",
                        message: "仅可以选中【已录取】状态的记录"
                    });
                    return;
                }
                let params = {
                    schoolMinorIds: this.schoolMinorIds
                };
                cancelMatriculate(params).then(res => {
                    if (res.code == 200) {
                        res.msg && this.$message({
                            type: "success",
                            message: res.msg
                        });
                        this.searchApplyNameList();
                    }
                });
            } else {
                this.$message.warning("请先点击筛选");
                return;
            }
        },
        getSelectRows() {
            this.isExistMatriculated = false;
            this.isNotMatriculated = false;
            this.chooseRows.length > 0 &&
                this.chooseRows.forEach(e => {
                    if (e.schoolMinorId != null) {
                        if (e.recruitStatus == 1) {
                            this.isExistMatriculated = true;
                        } else {
                            this.isNotMatriculated = true;
                        }
                        if (this.schoolMinorIds.length != 0) {
                            this.schoolMinorIds += "," + e.schoolMinorId;
                        } else {
                            this.schoolMinorIds += e.schoolMinorId;
                        }
                        this.updateParams.advancedLevel = e.advancedLevel;
                        this.updateParams.majorCode = e.minorMajor;
                        this.updateParams.minorSchool = e.minorSchool;
                    }
                });
        },
        batchMatriculateLastMinor() {
            if (!this.$checkMinor()) return;
            //校验辅修年级、进阶层次、辅修专业为必填项
            if (
                this.queryParams.grade == "" ||
                this.queryParams.grade == undefined
            ) {
                this.$message.warning("请选择辅修年级");
            } else if (
                this.queryParams.schoolId == "" ||
                this.queryParams.schoolId == undefined
            ) {
                this.$message.warning("请选择学校");
            } else if (
                this.form.minorMajorId == "" ||
                this.form.minorMajorId == undefined
            ) {
                this.$message.warning("请选择专业");
            } else if (
                this.form.name != "" ||
                this.form.studentNum != "" ||
                this.form.recruitStatus != ""
            ) {
                this.$message.warning("批量录取不支持输入姓名、学号、录取状态");
            } else {
                let params = {
                    minorGrade: this.queryParams.grade,
                    advanceLevel: this.form.advancedLevel,
                    majorId: this.form.minorMajorId,
                    minorSchool: this.queryParams.schoolId,
                    faultyCode: ""
                };
                batchMatriculate(params).then(res => {
                    if (res.code == 200) {
                        res.msg && this.$message({
                            type: "success",
                            message: res.msg
                        });
                        this.searchApplyNameList();
                    }
                }).catch(error=>{
                    if (typeof(error)=='string' && error.indexOf(201) > -1) {
                        this.searchApplyNameList();
                    }
                })
            }
        },
        matriculateLastMinor() {
            if (!this.$checkMinor()) return;
            if (this.allowOption) {
                this.schoolMinorIds = "";
                this.getSelectRows();
                if (this.schoolMinorIds.length == 0) {
                    this.$message({
                        type: "warning",
                        message: "请选择要录取的学生"
                    });
                    return;
                }
                if (this.isExistMatriculated) {
                    this.$message({
                        type: "warning",
                        message: "仅可以选中【未录取】状态的记录"
                    });
                    return;
                }
                let params = {
                    schoolMinorIds: this.schoolMinorIds,
                    advanceLevel: this.updateParams.advancedLevel,
                    majorId: this.updateParams.majorCode,
                    minorSchool: this.updateParams.minorSchool,
                    minorGrade: this.queryParams.grade
                };
                matriculate(params).then(res => {
                    if (res.code == 200) {
                        res.msg && this.$message({
                            type: "success",
                            message: res.msg
                        });
                        this.searchApplyNameList();
                    }
                }).catch(error=>{
                    if (typeof(error)=='string' && error.indexOf(201) > -1) {
                        this.searchApplyNameList();
                    }
                })
            } else {
                this.$message.warning("请先点击筛选");
                return;
            }
        },
        clearTabelData() {
            this.tableData = [];
            this.allowOption = false;
        },
        exportCurrentFormExcel() {
            if (!this.$checkMinor()) return;
            //导出当前表单的名单信息
            let params = {
                minorGrade: this.queryParams.grade,
                schoolCode: this.queryParams.schoolId,
                advanceLevel: this.form.advancedLevel,
                majorId: this.form.minorMajorId,
                name: this.form.name,
                studentNum: this.form.studentNum,
                recruitStatus: this.form.recruitStatus,
                exportType: "0"
            };
            let url = exportStudentListByAdmin(params);
            window.open(url);
        },
        exportSchoolOutMinorListExcel() {
            if (!this.$checkMinor()) return;
            //导出全部校外辅修的名单信息
            let params = {
                minorGrade: "",
                schoolCode: "",
                advanceLevel: "",
                majorId: "",
                name: "",
                studentNum: "",
                recruitStatus: "",
                exportType: "1"
            };
            let url = exportStudentListByAdmin(params);
            window.open(url);
        },
        //一页展示多少条
        handleSizeChange(pageSize) {
            this.queryParams.pageSize_ = pageSize;
            this.searchApplyNameList();
        }
    }
};
</script>
<style scoped>
.title-one {
    font-size: 18px;
    font-family: "微软雅黑";
    width: 8%;
    float: left;
}
.title-two {
    width: 11%;
    float: left;
    font-size: 14px;
    font-family: "微软雅黑";
    text-align: right;
    color: #4c5c70;
}
.formstyle {
    padding-bottom: 62px;
    border-bottom: 2px solid #d2d4d6;
}
.searchButton {
    float: right;
}
.minDiv {
    float: left;
    width: 34%;
}
.rightCol {
    margin-left: 1rem;
}
.preDiv {
    border-bottom: 1px solid #e4e7ed;
    border-top: 1px solid #e4e7ed;
    line-height: 33px;
}
.addDiv {
    text-align: right;
}
.preList {
    padding-top: 3px;
    border-bottom: 1px solid #ebebeb;
    padding-bottom: 3px;
}
.diagDiv {
    margin-top: 25px;
}
.tipsDiv {
    float: left;
    width: 83%;
    margin-top: 16px;
    font-size: 12px;
}
.addButon {
    float: right;
    margin-left: 4px;
    margin-bottom: 15px;
    margin-top: 15px;
}
.pageTatal {
    float: left;
    margin-left: 10px;
}
.titleButton {
    float: left;
    color: #226fe5;
    font-weight: bold;
    line-height: 32px;
}
.fontDiv {
    float: right;
    margin-left: 8px;
}
.pull-right {
    float: right;
}
.signFont {
    font-size: 16px;
    font-family: "微软雅黑";
    font-weight: bold;
    color: #8896a7;
    text-align: center;
    min-height: 500px;
    padding: 153px 0px;
}
.divbackground {
    margin: 0 auto;
    width: 134px;
    height: 160px;
    background-image: url(/static/images/no_data_background.png);
}
.preview-btn {
    position: relative;
    color: #2876c2;
    font-size: 12px;
}
.preview-btn:hover {
    color: #409eff;
    cursor: pointer;
    text-decoration: underline;
}
.preview-btn img {
    width: 100%;
    height: 100%;
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;
}
@-moz-document url-prefix() {
    /*只在firefox起作用*/
    input {
        box-shadow: none;
    }
}
</style>


